package com.guigu.qingcheng.servicegoods.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.goods.TbSku;
import com.guigu.qingcheng.pojo.order.TbOrderItem;
import com.guigu.qingcheng.servicegoods.service.TbSkuService;
import com.guigu.qingcheng.utils.R;
//import org.apache.http.HttpHost;
//import org.elasticsearch.action.bulk.BulkRequest;
//import org.elasticsearch.action.bulk.BulkResponse;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.index.IndexResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestClientBuilder;
//import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/sku")
public class TbSkuController {
    @Autowired
    TbSkuService skuService;


    //查询所有
    @GetMapping("findAll")
    public R findAll(){
        List<TbSku> list = this.skuService.list(null);
        if (list.size()>0)
            return R.ok().data("list",list);
        return R.error().data("list",list);
    }

    @GetMapping("/price")
    public Integer price(@RequestParam("id") String id){
        return this.skuService.findPrice(id);
    }




    //分页不带条件
    @GetMapping("findPage")
    public R findPage(@RequestParam("page") int page, @RequestParam("size") @PathVariable int size){
        //创建page对象
        Page<TbSku> pageTbSku = new Page<>(page,size);
        this.skuService.page(pageTbSku,null);

        long total = pageTbSku.getTotal();//总记录数
        List<TbSku> records = pageTbSku.getRecords(); //数据list集合
        if (total>0 && records.size()>0)
            return R.ok().data("total",total).data("rows",records);
        return R.error();
    }

    //条件查询不带分页
    @PostMapping("findList")
    public R findList(@RequestBody Map<String,Object> searchMap){
        List<TbSku> list = skuService.findList(searchMap);
        if (list.size()>0){
            return R.ok().data("list",list);
        }
        return R.error().data("list",list);
    }

    //条件查询带分页
    @PostMapping("findPage")
    public R pageTbSkuCondition(@RequestBody Map<String, Object> searchMap, @RequestParam("page") int page, @RequestParam("size") int size){
        Page<TbSku> pageTbSku = skuService.findPage(searchMap,page,size);
        if (pageTbSku.getRecords().size()>0){
            return R.ok().data("total",pageTbSku.getTotal()).data("rows",pageTbSku.getRecords());
        }
        return R.error().data("total",pageTbSku.getTotal()).data("rows",pageTbSku.getRecords());
    }

    //根据id查询
    @GetMapping("findById")
    public R findById(@RequestParam("id") String id){
        TbSku tbSku = new TbSku();
        tbSku = skuService.getById(id);
        return R.ok().data("sku",tbSku);
    }

    //根据id查询
    @GetMapping("findByIds")
    public TbSku findByIds(@RequestParam("id") String id){
        TbSku byId = skuService.getById(id);
        return byId;
    }

    //增加数据
    @PostMapping("add")
    public R addTbSku(@RequestBody TbSku tbSku){
        Boolean flag = skuService.save(tbSku);
        if (flag)
            return R.ok();
        return R.error();
    }
    //修改数据
    @PostMapping("update")
    public R updateTbSku(@RequestBody TbSku tbSku){
        boolean flag = this.skuService.updateById(tbSku);
        if (flag)
            return R.ok();
        return R.error();
    }

    //删除数据
    @GetMapping("delete")
    public R deleteTbSkuById(@RequestParam("id") String id){
        boolean flag = skuService.removeById(id);
        if (flag)
            return R.ok();
        return R.error();
    }

    @PostMapping("deductionStock")
    public boolean deductionStock(@RequestBody List<TbOrderItem> oderItemList) {
        return skuService.deductionStock(oderItemList);
    }
    /*@GetMapping("ljdelete")
    public R ljdelete(@RequestParam("id") String id){
        Boolean flag = this.skuService.ljdelete(id);
        if (flag)
            return R.ok().data("flag",true);
        return R.error().data("flag",false);
    }*/
}

